package com.example.superoa.model.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.KqRecessDao;
import com.example.superoa.model.mappers.RecessMapper;
import com.example.superoa.model.mappers.VacationMapper;
import com.example.superoa.model.pojos.KqRecess;

@Service
@Transactional(rollbackFor=Exception.class)
public class VacationService {
	@Autowired
	private VacationMapper map;
	
	@Autowired
	private RecessMapper r_map;
	
	@Autowired
	private KqRecessDao dao;
	private Shijian sj=new Shijian();
	
	public JSONArray selectAll() {
		return map.selectAll();
	}
	public JSONObject selectId(int id) {
		return map.selectId(id);
	}
	public Integer panduan(Integer lei,Integer type,Date kai,Date jie,Integer id) {
		if(lei==1) {
			return panduanyiguo(type,kai,jie);
		}else {
			return panduanshijian(type, kai, jie, id);
		}
	}
	//不能修改本月已过日期的假期
	public Integer panduanyiguo(Integer type,Date kai,Date jie) {
		List<Integer> list=sj.getyearmonth(new Date(sj.getNetworkTime()));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Integer lei=2;
		if(type==2) {
			lei=1;
		}
		for (String str : sj.getDayByMonth(list.get(0), list.get(1)-1, list.get(2), 2)) {
			Date day = null;
			try {
				day=sdf.parse(str);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(sj.belongCalendar(day, kai, jie,lei )) {
				
				return 1;
			}
		}
		return 0;
	}
	//判断输入时间
	public Integer panduanshijian(Integer type,Date kai,Date jie,Integer id) {
		JSONArray vacation=map.selectAllQi(id);
		for (Date day : sj.getDays(type,kai,jie)) {
			for (Object object : vacation) {
				JSONObject obj=(JSONObject)object;
				Integer s;
				if(obj.getInteger("RE_CLASS")==1) {
					s=2;
				}else {
					s=1;
				}
				if(sj.belongCalendar(day, obj.getDate("RE_KAI"), obj.getDate("RE_JIE"), s)) {
					System.err.println(obj+"---"+day);
					return 1;
				}
			}
		}
		return 0;
	}
	public void addjiqi(KqRecess pojo) {
		dao.save(pojo);
	}
	public void delect(int id) {
		dao.deleteById(id);
	}
	
}	
